Skip to main content

TMSchemaValidation

A schema validates the shape of a TableManager's data when the manager is created, so malformed data fails fast at construction rather than surfacing as a mysterious bug later.

Attaching a schema

Pass Config.Schema a T check (the T runtime type library is re-exported as TableManager.T). The initial data is validated against it at construction time; a mismatch errors from new.

local T = TableManager.T

local manager = TableManager.new({
	Player = { Name = "Alice", Level = 1 },
}, {
	Schema = T.interface {
		Player = T.interface {
			Name = T.string,
			Level = T.number,
		},
	},
})

Use T.strictInterface to also reject unexpected keys, and combinators like T.array(T.number) for collections:

Schema = T.strictInterface {
	Scores = T.array(T.number),
}

OnValidationFailed

Provide OnValidationFailed to observe failures. It is called with (path, value, err) before the error is raised, which is useful for logging or telemetry.

local manager = TableManager.new(data, {
	Schema = schema,
	OnValidationFailed = function(path, value, err)
		warn(`validation failed at {table.concat(path, ".")}: {err}`)
	end,
})

See also

Show raw api
{
    "functions": [],
    "properties": [],
    "types": [],
    "name": "TM Schema Validation",
    "desc": "A **schema** validates the shape of a [TableManager](/api/TableManager)'s data\nwhen the manager is created, so malformed data fails fast at construction rather\nthan surfacing as a mysterious bug later.\n\n## Attaching a schema\n\nPass `Config.Schema` a [`T`](/api/TableManager#T) check (the `T` runtime type\nlibrary is re-exported as `TableManager.T`). The initial data is validated\nagainst it at construction time; a mismatch errors from `new`.\n\n```lua\nlocal T = TableManager.T\n\nlocal manager = TableManager.new({\n\tPlayer = { Name = \"Alice\", Level = 1 },\n}, {\n\tSchema = T.interface {\n\t\tPlayer = T.interface {\n\t\t\tName = T.string,\n\t\t\tLevel = T.number,\n\t\t},\n\t},\n})\n```\n\nUse `T.strictInterface` to also reject unexpected keys, and combinators like\n`T.array(T.number)` for collections:\n\n```lua\nSchema = T.strictInterface {\n\tScores = T.array(T.number),\n}\n```\n\n## OnValidationFailed\n\nProvide `OnValidationFailed` to observe failures. It is called with\n`(path, value, err)` *before* the error is raised, which is useful for logging\nor telemetry.\n\n```lua\nlocal manager = TableManager.new(data, {\n\tSchema = schema,\n\tOnValidationFailed = function(path, value, err)\n\t\twarn(`validation failed at {table.concat(path, \".\")}: {err}`)\n\tend,\n})\n```\n\n---\n### See also\n\n- **[TM Getting Started](/api/TM%20Getting%20Started)** — the config table at a glance.\n- **[TM Opaque Values](/api/TM%20Opaque%20Values)** — telling the diff engine to leave certain values alone.\n- **[TM Listeners & Fire Modes](/api/TM%20Listeners%20&%20Fire%20Modes)** — observing data once it's validated.",
    "source": {
        "line": 60,
        "path": "lib/tablemanager/src/Docs/TM_Schema_Validation.luau"
    }
}